package com.example.easyutils.util;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.easyutils.R;
//清空input框
public class InputDel {
    EditText editText;
    ImageView delImageView;
    Context context;
    public InputDel(EditText editText, ImageView imageView, Context context) {
        this.editText = editText;
        this.delImageView = imageView;
        this.context = context;
    }
        //设置监听器
        public void initListener(){
            editText.addTextChangedListener(textWatcher);//监听edit变化
//        delImageView.setOnClickListener(this);
        }

        public TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (editText.getEditableText().length() >= 1){
//                    editText.setCompoundDrawablePadding(50);
                    Drawable drawableRight = context.getResources().getDrawable(R.drawable.del);
                    editText.setCompoundDrawablesWithIntrinsicBounds(null,
                            null, drawableRight, null);
//                    delImageView.setVisibility(View.VISIBLE);
                } else{
                    editText.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
//                    delImageView.setVisibility(View.GONE);
                }
            }
        };

    public void editDel(){
        editText.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // et.getCompoundDrawables()得到一个长度为4的数组，分别表示左右上下四张图片
                Drawable drawable = editText.getCompoundDrawables()[2];
                //如果右边没有图片，不再处理
                if (drawable == null)
                    return false;
                //如果不是按下事件，不再处理
                if (event.getAction() != MotionEvent.ACTION_UP)
                    return false;
                if (event.getX() > editText.getWidth()
                        - editText.getPaddingRight()
                        - drawable.getIntrinsicWidth()){
                    editText.setText("");
                }
                return false;
            }
        });
    }

//    }
}

//生成二维码
//https://api.66mz8.com/api/qr.php?text=http://http://116.62.147.89:8081/#/&size=216
